class Driver
{
    static getInstance()
    {
        if(!Driver.instance)
        {
            Driver.instance = new Driver();
        }
        return Driver.instance;
    }

    /**
     * 在地图上显示司机家的地理位置
     * @param drivers
     */
    render()
    {
        if(CONFIG.driverPositionShowStatus=='show')
        {
            return false;
        }
        let drivers = OBJECT.driverList;
        drivers.forEach( function ( value , index) {
               this.showDriverMarket( value )
        },this);
        CONFIG.driverPositionShowStatus='show'
    }

    /**
     * 显示司机家的标记
     * @param driver
     */
    showDriverMarket( driver ){
        let uluru = {lat: Number(driver.lat), lng: Number(driver.lng)};

        let contentString = '<div id="content">'+
            '<div id="siteNotice">'+
            '</div>'+
            '<h1 id="firstHeading" class="firstHeading">'+driver.nickname+'</h1>'+
            '<div id="bodyContent">'+
            '<p><b>End Location </b>'+driver.end_location+'<br/>'+driver.address+'</p>'+
            '</div>'+
            '</div>';
        let infowindow = new google.maps.InfoWindow({
            content: contentString
        });
        let icon = {
            url: "../static/images/driver-home.png", // url
            scaledSize: new google.maps.Size(50, 50), // scaled size
            origin: new google.maps.Point(0,0), // origin
            anchor: new google.maps.Point(0, 0) // anchor
        };
        OBJECT.driverMarketList[driver.id] = new google.maps.Marker({
            position: uluru,
            map: map,
            title: driver.nickname,
            strokeColor: 'green',
            icon:icon
        });
        let obj = OBJECT.driverMarketList[driver.id];   //浅复制一个对象
        OBJECT.driverMarketList[driver.id].addListener('click', function() {
            infowindow.open(map, obj);
        });
    }

    /**
     * 隐藏司机地址
     */
    hide(){
        if(CONFIG.driverPositionShowStatus=='hide')
        {
            return;
        }
        let marker = OBJECT.driverMarketList;
        marker.forEach( function ( value ,index ) {
            OBJECT.driverMarketList[index].setVisible(false);
        });
        CONFIG.driverPositionShowStatus='hide'
    }
    getAssignedDriver()
    {
        let ret = [];
        for( let routerId in OBJECT.routerPonits )
        {
            if(OBJECT.routerPonits[routerId].driverId)
            {
                ret.push(OBJECT.routerPonits[routerId].driverId)
            }
        }
        return ret;
    }

    /**
     * 通过routerId 获取司机信息
     * @param routerId
     * @returns {Array}
     */
    getDriverByRouterId( routerId)
    {
        let ret = null;
        let driverId = OBJECT.routerPonits[routerId].driverId;
        OBJECT.driverList.forEach( function ( value ,index ) {
             if( value.driver_id ==  driverId )
             {
                 ret = value;
             }
        })
        return ret;
    }
    /**
     * 通过DriverId 获取司机信息
     * @param routerId
     * @returns {Array}
     */
    getDriverById( DriverId )
    {
        let ret = null;
        OBJECT.driverList.forEach( function ( value ,index ) {
            if( value.driver_id ==  DriverId )
            {
                ret = value;
            }
        })
        return ret;
    }

    /**
     * 获取司机最后一站
     * @param routerId
     * @returns {boolean}
     */
    getLastPoint(routerId)
    {
         let ret;
         let driverId = OBJECT.routerPonits[routerId].driverId;
         if( !driverId )
         {

             return null;
         }

        OBJECT.driverList.forEach( function ( value ,index ) {
              if( value.driver_id == driverId )
              {

                   if( value.end_location=="Home" ){
                       ret =  {location:"Home",lat:value.lat,lng:value.lng};
                   }else{
                       ret = {location:"WareHouse",lat:CONFIG.wareHouseCenter.lat,lng:CONFIG.wareHouseCenter.lng};
                   }
              }
        });
         return ret;

    }

}